1277D - Let's Play the Words - CodeForces Solution


data structures hashing implementation math *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
 
using namespace std;
typedef long long ll;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
 
int main()
{
    ll t;
    cin>>t;
    while(t--){
        vector<ll>v,v1;
        ll n;
        cin>>n;
        string s[n];
        map<string,ll>mpp;
        ll a=0,b=0,c=0,d=0;
        for(ll i=0;i<n;i++){
            cin>>s[i];
            ll x=s[i][0]-'0';
            ll y=s[i][s[i].length()-1]-'0';
            mpp[s[i]]++;
            
            if(x==0&&y==0){
                a++;
            }
            else if(x==0&&y==1){
                b++;
               
            }
            else if(x==1&&y==0){
                c++;
               
            }
            else{
                d++;
            }
        }
        for(ll i=0;i<n;i++){
            ll x=s[i][0]-'0';
            ll y=s[i][s[i].length()-1]-'0';
          if(x==0&&y==1){
              string p=s[i];
              reverse(s[i].begin(),s[i].end());
              if(mpp[s[i]]==0){
                  v.push_back(i+1);
              }
              s[i]=p;
          }
          else if(x==1&&y==0){
              string p=s[i];
              reverse(s[i].begin(),s[i].end());
              if(mpp[s[i]]==0){
                  v1.push_back(i+1);
              }
              s[i]=p;
          }
        }
        if(a!=0&&d!=0&&(b+c)==0){
            cout<<-1<<endl;
        }
        else{
            vector<ll>ans;
            ll flg=0;
            if(a==0&&b==0&&c==0){
                
            }
            else if(b==0&&c==0&&d==0){
                
           }
            else if(a==0&&d!=0){
              
                if(b>c){
                    ll p=b-c;
                    ll k=p/2;
                   
                    if(v.size()<k){
                        flg=1;
                    }
                    else{
                    for(ll i=0;i<k;i++){
                        ans.push_back(v[i]);
                    }
                    }
                }
               else{
                   ll p=c-b;
                   ll k=p/2;
                   k=max(0LL,k);
                   if(v1.size()<k){
                       flg=1;
                   }
                   else{
                   for(ll i=0;i<k;i++){
                       ans.push_back(v1[i]);
                   }}
                   
               }
                  
                      
               
            }
            else if(a!=0&&d==0){
                
                if(c>b){
                    ll p=c-b;
                    ll k=p/2;
                    
                    if(v1.size()<k){
                        flg=1;
                    }
                    else{
                    for(ll i=0;i<k;i++){
                        ans.push_back(v1[i]);
                    }
                    }
                }
               else{
                   ll p=b-c;
                   ll k=p/2;
                   k=max(k,0LL);
                   if(v.size()<k){
                       flg=1;
                   }
                   else{
                       
                   for(ll i=0;i<k;i++){
                       ans.push_back(v[i]);
                   }
                   }
                   
               }
                  
            }
            else if(a==0&&d==0){
                ll x=abs(b-c);
                ll p=x/2;
                if(b>c){
                    if(v.size()<p){
                        flg=1;
                    }
                    else{
                    for(ll i=0;i<p;i++){
                        ans.push_back(v[i]);
                    }
                    }
                }
                else{
                    if(v1.size()<p){
                        flg=1;
                    }
                    else{
                      for(ll i=0;i<p;i++){
                        ans.push_back(v1[i]);
                    }
                    }
                }
            }
            else{
                
                if(b==c){
                    
                }
                else if(b>c){
                    ll g=b-c;
                    ll k=g/2;
                    if(v.size()<k){
                        flg=1;
                    }
                    else{
                    for(ll i=0;i<k;i++){
                        ans.push_back(v[i]);
                    }
                    }
                }
                else{
                    ll g=c-b;
                    ll k=g/2;
                    if(v1.size()<k){
                        flg=1;
                    }
                    else{
                    for(ll i=0;i<k;i++){
                        ans.push_back(v1[i]);
                    }
                    }
                }
                
            }
            if(flg==0){
            cout<<ans.size()<<endl;
            for(ll i=0;i<ans.size();i++){
                cout<<ans[i]<<" ";
            }
            cout<<endl;}
            else{
                cout<<-1<<endl;
            }
        }
        
        
        
        
    }
}
 


Comments

Submit
0 Comments
More Questions

1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training
1646C - Factorials and Powers of Two
596A - Wilbur and Swimming Pool
1462B - Last Year's Substring
1608B - Build the Permutation
1505A - Is it rated - 2
169A - Chores
765A - Neverending competitions
1303A - Erasing Zeroes
1005B - Delete from the Left
94A - Restoring Password
1529B - Sifid and Strange Subsequences
1455C - Ping-pong
1644C - Increase Subarray Sums
1433A - Boring Apartments
1428B - Belted Rooms
519B - A and B and Compilation Errors
1152B - Neko Performs Cat Furrier Transform
1411A - In-game Chat
119A - Epic Game
703A - Mishka and Game
1504C - Balance the Bits
988A - Diverse Team
1312B - Bogosort
1616B - Mirror in the String
1660C - Get an Even String
489B - BerSU Ball
977C - Less or Equal